﻿using BCCommon;
using BCData.OA.PageBusiness.PageBusinessConfig;
using BCData.Sim.Company.Company;
using BCDto.OA.PageBusiness.PageBusinessConfig;
using BCEntity.OA.PageBusiness.PageBusinessConfig;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.PageBusiness.PageBusinessConfig
{
    public class PageBusinessConfigService : IPageBusinessConfigService
    {
        private readonly IApplicationContextService applicationContextService;
        private readonly IPageBusinessConfigData pageBusinessConfigData;
        private readonly ICompanyData companyData;
        public PageBusinessConfigService(IApplicationContextService applicationContextService,
            IPageBusinessConfigData pageBusinessConfigData, 
            ICompanyData companyData)
        {
            this.applicationContextService = applicationContextService;
            this.pageBusinessConfigData = pageBusinessConfigData;
            this.companyData = companyData;
        }

        public bool Exists(long pageBusinessConfigId)
        {
            return this.pageBusinessConfigData.Exists(pageBusinessConfigId).GetAwaiter().GetResult();
        }

        public bool Exists(string pageBusinessCode)
        {
            return this.pageBusinessConfigData.Exists(pageBusinessCode).GetAwaiter().GetResult();
        }

        public PageBusinessConfigDto Add(PageBusinessConfigRequestDto requestDto)
        {
            if (this.pageBusinessConfigData.Exists(requestDto.PageBusinessCode).GetAwaiter().GetResult())
            {
                throw new ArgumentException("此页面业务编码已存在");
            }
            var entity = requestDto.As<PageBusinessConfigEntity>();
            entity.CompanyId = applicationContextService.Admin.CompanyId;
            entity.CompanyName = applicationContextService.Admin.CompanyName;
            return this.pageBusinessConfigData.Add(entity).GetAwaiter().GetResult().As<PageBusinessConfigDto>();
        }
        public PageBusinessConfigDto Update(PageBusinessConfigPutDto putDto)
        {
            if (!this.Exists(putDto.PageBusinessConfigId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var oldEntity = this.pageBusinessConfigData.GetEntity(putDto.PageBusinessConfigId).GetAwaiter().GetResult();
            if (oldEntity.PageBusinessCode != putDto.PageBusinessCode)
            {
                if (this.pageBusinessConfigData.Exists(putDto.PageBusinessCode).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此页面业务编码已存在");
                }
            }
            var entity = putDto.As<PageBusinessConfigEntity>();
            return this.pageBusinessConfigData.Update(entity).GetAwaiter().GetResult().As<PageBusinessConfigDto>();
        }

        public bool Delete(long pageBusinessConfigId)
        {
            if (!this.Exists(pageBusinessConfigId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.pageBusinessConfigData.Delete(pageBusinessConfigId).GetAwaiter().GetResult();
        }

        public PageBusinessConfigDto GetEntity(long pageBusinessConfigId)
        {
            return this.pageBusinessConfigData.GetEntity(pageBusinessConfigId).GetAwaiter().GetResult().As<PageBusinessConfigDto>();
        }

        public IEnumerable<PageBusinessConfigDto> GetList(int companyId)
        {
            return this.pageBusinessConfigData.GetList(companyId).GetAwaiter().GetResult().As<IEnumerable<PageBusinessConfigDto>>();
        }

        public PageableList<PageBusinessConfigDto> Query(int companyId, string pageBusinessName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.pageBusinessConfigData.Query(companyId, pageBusinessName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<PageBusinessConfigDto>
            {
                Items = list.As<IEnumerable<PageBusinessConfigDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
